package hk.ust.comp3111.ustnutrition;

import hk.ust.comp3111.ustnutrition.data.DataAccessor;
import hk.ust.comp3111.ustnutrition.data.db.Food;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.ImageView;

public class MiscUtils
{
	static final String TAG = "hk.ust.comp3111.ustnutrition.MiscUtils";

	public static float dpToPx(Context context, float dp)
	{
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, dm);
	}

	public static float spToPx(Context context, float sp)
	{
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, dm);
	}

	public static <T> ArrayList<T> iteratorToList(Iterator<T> it) {
		ArrayList<T> list = new ArrayList<T>();
		while(it.hasNext())
			list.add(it.next());
		return list;
	}

	public static String get(Map<String, Object> map, String key, String def) {
		Object obj = map.get(key);
		if(obj != null && obj instanceof String)
			return (String)obj;
		else
			return def;
	}

	public static int get(Map<String, Object> map, String key, int def) {
		Object obj = map.get(key);
		if(obj != null && obj instanceof Integer)
			return (Integer)obj;
		else if(obj != null && obj instanceof String)
		{
			try
			{
				return Integer.parseInt((String)obj);
			}
			catch(NumberFormatException e)
			{
				return def;
			}
		}
		else
			return def;
	}

	public static boolean get(Map<String, Object> map, String key, boolean def) {
		Object obj = map.get(key);
		if(obj != null && obj instanceof Boolean)
			return (Boolean)obj;
		else if(obj != null && obj instanceof String)
		{
			try
			{
				return Boolean.parseBoolean((String)obj);
			}
			catch(NumberFormatException e)
			{
				return def;
			}
		}
		else
			return def;
	}

	public static double get(Map<String, Object> map, String key, double def) {
		Object obj = map.get(key);
		if(obj != null && obj instanceof Double)
			return (Double)obj;
		else if(obj != null && obj instanceof Integer)
			return (Integer)obj;
		else if(obj != null && obj instanceof Long)
			return (Long)obj;
		else if(obj != null && obj instanceof String)
		{
			try
			{
				return Double.parseDouble((String)obj);
			}
			catch(NumberFormatException e)
			{
				return def;
			}
		}
		else
			return def;
	}

	public static long get(Map<String, Object> map, String key, long def) {
		Object obj = map.get(key);
		if(obj != null && obj instanceof Long)
			return (Long)obj;
		else if(obj != null && obj instanceof Integer)
			return (Integer)obj;
		else if(obj != null && obj instanceof String)
		{
			try
			{
				return Long.parseLong((String)obj);
			}
			catch(NumberFormatException e)
			{
				return def;
			}
		}
		else
			return def;
	}

	public static DownloadImageTask LoadFoodImage(ImageView view, Food food) {
		Drawable dummy = view.getResources().getDrawable(R.drawable.dummy_food);
		if(food.getPhoto() == null)
		{
			view.setImageDrawable(dummy);
			return null;
		}
		else
		{
			return LoadImage(view, DataAccessor.Instance.getResource(food.getPhoto()), dummy);
		}
	}

	public static DownloadImageTask LoadImage(ImageView view, String url, Drawable failImg) {
		if(url != null) {
			DownloadImageTask task = new DownloadImageTask(view, failImg);
			task.execute(url);
			return task;
		}
		return null;
	}

	public static int getDate(Date date) {
		return (int)(date.getTime() / (1000 * 3600 * 24));
	}
}
